// index.js
// 获取应用实例
const app = getApp()

Page({
 
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    config: {},
    userLogin: false
  },

  baseInfo() {
    let that = this;
    app.req({
      url: "/common/static/base",
      success(res) {
        if (200 == res.code) {
          that.toggle("config", res.result);
        }
      }
    })
  },
  // 事件处理函数
  toFunction(e) {
    let that = this;
    var f;
    switch (e.currentTarget.dataset.type) {
      case "sign":
        f = this.sign;
        break;
      case "view":
        f = this.view;
        break;
      case "order":
        f = this.order;
        break;
    }
    that.login(function (res) {
      f();
    }, true)
  },
  sign() {
    let that = this;
    if (that.data.config.clock) {
      app.showToast({
        title: '您已经签过到',
        success() {
          that.refreshData();
        }
      })
      return
    }
    app.req({
      url: "/clock/busPointsClock/add",
      method: 'post',
      success(res) {
        if (200 == res.code) {
          app.showToast({
            title: '签到成功',
            success() {
              that.refreshData();
            }
          })
        } else {
          app.showToast({
            title: res.message,
            icon: 'none',
            success() {
              that.refreshData();
            }
          })
        }
      }
    })
  },
  refreshData() {
    let that = this;
    that.login(function (res) {
      that.toggle("userLogin", true);
      that.baseInfo();
    })
  },
  view() {
    wx.showModal({
      title: '电脑端下单',
      content: '请用电脑浏览器登录下单\nhttp://180.100.211.39:6672/',
      showCancel: false,
    })
  },
  order() {
    wx.navigateTo({
      url: '/pages/order/newOrder/newOrder',
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  login(f, login) {
    let that = this;
    app.getUserInfo({
      success(res) {
        f && f(res);
      },
      error() {
        if (login) {
          wx.showModal({
            title: '提示',
            content: '请您登录之后继续操作',
            confirmText: '登录',
            success({
              confirm
            }) {
              if (confirm) {
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              }
            }
          })
        }
      }
    })
  },
  onLoad() {

  },
  onShow() {
    this.refreshData();
  },
  onPullDownRefresh() {
    // 上拉刷新 if (!this.loading) {
      this.refreshData();
    },
    toRulePage(){
      wx.navigateTo({
        url: '/pages/pointRule/pointRule'
      })
    },
    toOtherService(){
      wx.navigateTo({
        url: '/pages/otherSer/otherSer'
      })
    }
})